﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace MediaBrowser.CustomWindow
{
    /// <summary>
    /// Logique d'interaction pour CustomWindowDialog.xaml
    /// </summary>
    internal partial class CustomWindowDialog : AnimatedWindow
    {
        public String TxMessage
        {
            get
            {
                return txMessage.Text;
            }
            set
            {
                txMessage.Text = value;
                if (txMessage.Text.Length > 31)
                {
                    double w = 350 + ((((350 * txMessage.Text.Length) / 31) - 350) / 18);
                    double h = 130 + ((((130 * txMessage.Text.Length) / 31) - 130) / 18);

                    double ws = 270 + ((((270 * txMessage.Text.Length) / 31) - 270) / 18);

                    if (w <= 800)
                    {
                        this.Width = w;
                        this.Height = h;
                        this.scrollViewer.Width = ws;
                    }
                    else
                    {
                        this.Width = 800;
                        this.Height = 400;
                        this.scrollViewer.Width = 720;
                    }
                }
            }
        }

        private MessageBoxResult messageBoxResult;
        public MessageBoxResult MessageBoxResult
        {
            get
            {
                return messageBoxResult;
            }
            private set
            {
                messageBoxResult = value;
            }
        }

        private MessageBoxButton button;
        public MessageBoxButton Button
        {
            get
            {
                return button;
            }
            set
            {
                button = value;

                BtOk.Visibility = Visibility.Collapsed;
                BtCancel.Visibility = Visibility.Collapsed;
                BtYes.Visibility = Visibility.Collapsed;
                BtNo.Visibility = Visibility.Collapsed;
                BtCancel2.Visibility = Visibility.Collapsed;
                if (button == MessageBoxButton.OK)
                {
                    BtOk.Visibility = Visibility.Visible;
                }
                else if (button == MessageBoxButton.OKCancel)
                {
                    BtOk.Visibility = Visibility.Visible;
                    BtCancel.Visibility = Visibility.Visible;
                }
                else if (button == MessageBoxButton.YesNo)
                {
                    BtYes.Visibility = Visibility.Visible;
                    BtNo.Visibility = Visibility.Visible;
                }
                else if (button == MessageBoxButton.YesNoCancel)
                {
                    BtYes.Visibility = Visibility.Visible;
                    BtNo.Visibility = Visibility.Visible;
                    BtCancel2.Visibility = Visibility.Visible;
                }
            }
        }

        private MessageBoxImage iconMessage;
        public MessageBoxImage IconMessage
        {
            get
            {
                return iconMessage;
            }
            set
            {
                iconMessage = value;
                imgMessage.Width = 70;
                if (iconMessage == MessageBoxImage.Asterisk)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_info.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_info.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }
                else if (iconMessage == MessageBoxImage.Error)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_error.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_error.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }
                else if (iconMessage == MessageBoxImage.Exclamation)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_warning.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_warning.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }
                else if (iconMessage == MessageBoxImage.Hand)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_error.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_error.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }
                else if (iconMessage == MessageBoxImage.Information)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_info.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_info.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }
                else if (iconMessage == MessageBoxImage.None)
                {
                    imgMessage.Source = null;
                    imgMessage.Width = 0;
                }
                else if (iconMessage == MessageBoxImage.Question)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_question.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_question.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }
                else if (iconMessage == MessageBoxImage.Stop)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_stop.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_stop.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }
                else if (iconMessage == MessageBoxImage.Warning)
                {
                    imgMessage.Source = new BitmapImage(new Uri("Images/messagebox_warning.png", UriKind.RelativeOrAbsolute));
                    Uri iconUri = new Uri("pack://application:,,,/MediaBrowser.CustomWindow;Component/Images/messagebox_warning.ico", UriKind.RelativeOrAbsolute);
                    this.Icon = BitmapFrame.Create(iconUri);
                }

            }
        }

        public CustomWindowDialog()
        {
            InitializeComponent();
            MessageBoxResult = MessageBoxResult.No;
            Button = MessageBoxButton.OK;
            IconMessage = MessageBoxImage.None;
        }

        private void BtOk_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.OK;
            Close();
        }

        private void BtCancel_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Cancel;
            Close();
        }

        private void BtYes_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Yes;
            Close();
        }

        private void BtNo_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.No;
            Close();
        }

        private void BtCancel2_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Cancel;
            Close();
        }
    }
}
